package org.manusoft.refill.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.manusoft.refill.domain.TDriverDetailsRf;
import org.manusoft.refill.domain.TRefillingDetailsRf;
import org.manusoft.refill.domain.TVehicleDetailsRf;
import org.manusoft.refill.dto.AllotmentDo;
import org.manusoft.refill.dto.DriverDO;
import org.manusoft.refill.dto.VehicleDO;
import org.manusoft.refill.service.AllotmentService;
import org.manusoft.refill.service.DriverService;
import org.manusoft.refill.service.VehicleService;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("createAllotment")
@Scope("view")
public class CreateAllotment implements Serializable{
 
	private boolean skip;  
	
	private AllotmentDo  allotmentDo;
	
	private AllotmentService allotmentService;
	
	@Autowired
	private DriverService driverService;	
	
	@Autowired
	private VehicleService vehicleService;
	
	public CreateAllotment() {
		allotmentDo=new AllotmentDo();
	}
	
	
	
	public List<String> getDriverbyDLNo(String dlNo) {
        List<TDriverDetailsRf> foundDrivers = driverService.findByDlNoAutoComplete(dlNo);
        List<String> titles = new ArrayList<String>();
        for (TDriverDetailsRf m : foundDrivers) {
            titles.add(m.getDriverDlNo());
        }
        return titles;
    }
	
	 public List<String> getVehiclesByRegNum(String regNum) {
	        List<TVehicleDetailsRf> foundVehicles = vehicleService.findByRegNumAutoComplete(regNum);
	        List<String> titles = new ArrayList<String>();

	        for (TVehicleDetailsRf m : foundVehicles) {
	            titles.add(m.getVehCharRegno());
	        }

	        return titles;
	    }
	 
	 
	public void save(ActionEvent actionEvent) {
		TRefillingDetailsRf refillingDetailsRf = new TRefillingDetailsRf();
		allotmentService.createAllotment(refillingDetailsRf);
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Allotment is saved");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		refillingDetailsRf = new TRefillingDetailsRf();
	}	
	
	public void setDriver(SelectEvent event) {
		TDriverDetailsRf driver= driverService.findByDlNo(event.getObject().toString());
		DriverDO driverDo  =new DriverDO();
		driverDo.setDriverName(driver.getDriverName());
		driverDo.setDriverAddress(driver.getDriverAddress());
		driverDo.setDriverDlNo(driver.getDriverDlNo());
		driverDo.setDriverDlExpDate(new java.sql.Date(driver.getDriverDlExpDate().getTime()));
		driverDo.setIsBlacklisted(driver.getIsBlacklisted());
		driverDo.setDriverMobileNo(driver.getDriverMobileNo());
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Driver is selected you may proceed to next step");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		allotmentDo.setDriverDO(driverDo);
		
	}
	
	
	public void setVechile(SelectEvent event) {
		TVehicleDetailsRf vehicle= vehicleService.findByRegNum(event.getObject().toString());
		VehicleDO vehicleDetailsRf  =new VehicleDO();
		vehicleDetailsRf.setVehCharInsurance(vehicle.getVehCharInsurance());
		vehicleDetailsRf.setVehCharMake(vehicle.getVehCharMake());
		vehicleDetailsRf.setVehCharModel(vehicle.getVehCharModel());
		vehicleDetailsRf.setVehCharOwner(vehicle.getVehCharOwner());
		vehicleDetailsRf.setVehCharRegno(vehicle.getVehCharRegno());
		vehicleDetailsRf.setVehDateInsurexpiry(vehicle.getVehDateInsurexpiry());
		vehicleDetailsRf.setVehDateRegdate(vehicle.getVehDateRegdate());
		vehicleDetailsRf.setVehNumId(vehicle.getVehNumId());
		vehicleDetailsRf.setVehNumTonnage(vehicle.getVehFillingCapacity());
		
		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Vechile is selected you may proceed to next step");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		allotmentDo.setVehicleDO(vehicleDetailsRf);
		
	}

	public AllotmentDo getAllotmentDo() {
		return allotmentDo;
	}

	public void setAllotmentDo(AllotmentDo allotmentDo) {
		this.allotmentDo = allotmentDo;
	}
	
	
	public boolean isSkip() {
		return skip;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}
	
	public String onFlowProcess(FlowEvent event) {
		if(skip) {
			skip = false;	//reset in case user goes back
			return "confirm";
		}
		else {
			if(event.getOldStep().equalsIgnoreCase("driver")){
				if(allotmentDo.getDlNo().isEmpty()){
					FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Driver is not selected");
					FacesContext.getCurrentInstance().addMessage(null, facesMessage);
					return event.getOldStep();
				}
			}
			return event.getNewStep();
		}
	}
	
	
}